using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace CustomControlInProject
{
    public partial class CustomControl1 : Control
    {
        public CustomControl1()
        {
            
        }   
        protected override void OnPaint(PaintEventArgs pe)
        {
            AddControl();
            base.OnPaint(pe);
        }
        #region myCode
        ListView lvData;
        Button btnView;
        Label label1;
        void AddControl()
        {

            lvData = new ListView();
            btnView = new Button();
            label1 = new Label();
            this.SuspendLayout();
          
            lvData.Location = new Point(10, 12);
            lvData.Name = "lvData";
            lvData.Size = new Size(288, 239);
            lvData.TabIndex = 0;
            lvData.UseCompatibleStateImageBehavior = false;
          
            btnView.Location = new Point(10, 260);
            btnView.Name = "btnView";
            btnView.Size = new Size(75, 23);
            btnView.TabIndex = 1;
            btnView.Text = "View";
            btnView.UseVisualStyleBackColor = true;
            btnView.Click += new System.EventHandler(btnView_Click);
          
            label1.AutoSize = true;
            label1.Location = new Point(92, 260);
            label1.Name = "label1";
            label1.Size = new Size(35, 13);
            label1.TabIndex = 2;
            label1.Text = "label1";
          
            Controls.Add(label1);
            Controls.Add(btnView);
            Controls.Add(lvData);
            Name = "ViewFile";
            Size = new Size(309, 286);
            ResumeLayout(false);
            PerformLayout();        
        }
        private string strPath="";

        public string Path
        {
            get { return strPath; }
            set { strPath = value; }
        }
        private void btnView_Click(object sender, EventArgs e)
        {
            try
            {
                if (strPath==null) strPath = "C:\\";
                DirectoryInfo dir = new DirectoryInfo(strPath);
                lvData.Columns.Add("Name", 100, 0);
                lvData.Columns.Add("Size", 50, 0);
                lvData.Columns.Add("Date", 70, 0);
                lvData.View = View.Details;
                ListViewItem item1;

                foreach (FileInfo d in dir.GetFiles("*.*"))
                {

                    item1 = new ListViewItem(d.Name);
                    item1.SubItems.Add(d.Length.ToString());
                    item1.SubItems.Add(d.CreationTime.ToLongDateString());
                    lvData.Items.Add(item1);
                }
                label1.Text = strPath;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
         
        #endregion myCode
    }
}
